﻿#include "stdafx.h"
#include "UserInit.h"
#include "MobileGameDlg.h"
#include "tlhelp32.h"

CIniFile g_IniFile;
CIniFile g_AccountFile;
CIniFile g_RoleNameFile;
CIniFile g_MoneyFile;
CIniFile g_TaskSpeedFile;
CIniFile g_ChangHaoStateFile;//成号状态

Cdmsoft *m_GlobalDM;//全局大漠
int m_GNum;
bool m_GState=true;
int m_GCount;
float m_fSim=0.90;//相似度
Cdmsoft *m_DM[20];
CLuck g_cLuck;
long m_GGameHwnd;
CString m_SimulatorPath;
CString g_Dnplayer2Path;
CString g_strDesktopPath;

ScriptSetupInfo m_ScriptSet[20];

CString NumberToString(LONG Number)
{
	CString sTring;
	sTring.Format(_T("%ld"), Number);
	return sTring;
}

BOOL FolderExists(CString s)
{
	DWORD attr;
	attr = GetFileAttributes(s);
	return (attr != (DWORD)(-1)) &&
		(attr & FILE_ATTRIBUTE_DIRECTORY);
}
//创建一个目录
BOOL CreateDir(CString P)
{
	int len = P.GetLength();
	if (len < 2) return false;

	if ('\\' == P[len - 1])
	{
		P = P.Left(len - 1);
		len = P.GetLength();
	}
	if (len <= 0) return false;

	if (len <= 3)
	{
		if (FolderExists(P))return true;
		else return false;
	}

	if (FolderExists(P))return true;

	CString Parent;
	Parent = P.Left(P.ReverseFind('\\'));

	if (Parent.GetLength() <= 0)return false;

	BOOL Ret = CreateDir(Parent);

	if (Ret)
	{
		SECURITY_ATTRIBUTES sa;
		sa.nLength = sizeof(SECURITY_ATTRIBUTES);
		sa.lpSecurityDescriptor = NULL;
		sa.bInheritHandle = 0;
		Ret = (CreateDirectory(P, &sa) == TRUE);
		return Ret;
	}
	else
		return FALSE;
}

//分割字符串
void StringSplit(CString source, CStringArray& dest, char division)
{
	if (source.IsEmpty())
		return;
	dest.RemoveAll();
	for (int i = 0; i < source.GetLength(); i++)
	{
		if (source.GetAt(i) == division)
		{
			dest.Add(source.Left(i));
			source.Delete(0, i + 1);
			i = 0;
		}
	}
	dest.Add(source);
}



DWORD RandomNumber(DWORD dwNumber1, DWORD dwNumber2)
{
	DWORD dwTemp;
	__asm
	{
		push ecx
		push edx
		rdtsc
		mov ecx, 0x17
		mul ecx
		add eax, 0x7
		mov ecx, dwNumber2//ss:[ebp + 0c]
		sub ecx, dwNumber1//ss : [ebp + 8]
		inc ecx
		xor edx, edx
		div ecx
		add edx, dwNumber1//ss : [ebp + 8]
		mov dwTemp, edx
		pop edx
		pop ecx
	}
	return dwTemp;
}

DWORD WINAPI StartScript1()
{
	return 0;
}

DWORD WINAPI StartScript(LPVOID lParam)
{
	ScriptSetupInfo* pSSI = (ScriptSetupInfo*)lParam;

	int iFreeDmIndex = pSSI->m_iDmIndex;
	int iIndex = pSSI->m_iControlIndex;

	//OpenSimulator(iFreeDmIndex);

	g_MobileGameDlg->InitUIGameInfo(iIndex, m_ScriptSet[iFreeDmIndex].m_GameInfoSt);//初始化一些信息


	g_MobileGameDlg->m_list_info.SetItemText(iIndex, 8, NumberToString(iFreeDmIndex));//模拟器序号

	long hHandle = NULL;
	CString strSimulatorTitleName;
	if (iFreeDmIndex == 0)
	{
		strSimulatorTitleName = L"雷电模拟器";
	}
	else
	{
		strSimulatorTitleName.Format(L"雷电模拟器-%d", iFreeDmIndex);
	}

	while (true)
	{
		hHandle = (long) ::FindWindow(NULL, strSimulatorTitleName);
		DbgOut(L"[sdn] hHandle:%d", hHandle);
		if ((int)hHandle)
		{
			break;
		}
		Sleep(3000);
	}

	//排序窗口
	DbgOut(L"[sdn] hHandle:%d", hHandle);
	//if (g_pPage1->m_radio_tran_expan.GetCheck())
	//{
	//	g_pPage1->OnBnClickedBtnHengxiang();
	//}
	//else
	//{
	//	g_pPage1->OnBnClickedBtnZhongxiang();
	//}


	g_pPage1->OnBnClickedBtnHengxiang();

	BOOL bRet = RegisterDM(iFreeDmIndex);
	if (!bRet)
	{
		AfxMessageBox(L"注册单个大漠失败");
		return 0;
	}

	g_MobileGameDlg->m_list_info.SetItemText(iIndex, 10, L"运行中");
	m_DM[iFreeDmIndex]->SetWindowState((long)hHandle, 1);

	//long hGameHandle = GetSubWindow(hHandle);
	long hGameHandle = m_DM[iFreeDmIndex]->GetWindow(hHandle, 1);
	DbgOut(L"[sdn] 子窗口句柄:%d", hGameHandle);
	while (true)
	{
		Sleep(3000);
		hGameHandle = m_DM[iFreeDmIndex]->GetWindow(hHandle, 1);
		if (hGameHandle != 0)
		{
			break;
		}
		DbgOut(L"[sdn] ---子窗口句柄:%d", hGameHandle);
	}

	//m_ScriptSet[iFreeDmIndex].m_iDmIndex = iFreeDmIndex;//大漠序号
	//m_ScriptSet[iFreeDmIndex].m_iControlIndex = iIndex;//控制台序号
	m_ScriptSet[iFreeDmIndex].m_hGameHandle = hGameHandle;
	//m_ScriptSet[iFreeDmIndex].m_hControlHandle = (long)g_MobileGameDlg->m_hWnd;//用于发送消息
	DbgOut(L"[sdn] 绑定 le");

	g_MobileGameDlg->m_list_info.SetItemText(iIndex, 1, NumberToString(hGameHandle));

	//m_DM[iFreeDmIndex]->SetWindowState((long)hGameHandle, 1);

	long dm_ret = m_DM[iFreeDmIndex]->BindWindowEx((long)hGameHandle,
		L"gdi", L"windows3", L"windows",
		NULL,
		0);

	/*	long dm_ret = m_DM[iFreeDmIndex]->BindWindowEx((long)hGameHandle,
	"gdi",
	"windows",
	"windows",
	NULL,
	0);*/
	//long dm_ret = aDm[nID]->BindWindowEx(lHwnd,
	//	"dx.graphic.opengl",
	//	"dx.mouse.position.lock.api|dx.mouse.clip.lock.api|dx.mouse.input.lock.api|dx.mouse.state.api|dx.mouse.api|dx.mouse.cursor",
	//	"dx.keypad.input.lock.api|dx.keypad.state.api|dx.keypad.api",
	//	NULL,
	//long dm_ret = m_DM[iFreeDmIndex]->BindWindowEx((long)hGameHandle, "dx.graphic.opengl", "windows", "windows", "dx.public.down.cpu", 0);
	Sleep(2000);
	if (dm_ret <= 0)
	{
		AfxMessageBox(L"游戏绑定失败!");
		ClearOneDM(iFreeDmIndex);
		return 0;
	}

	int iRate = g_IniFile.GetInt(_T("Set"), _T("CPU"));
	if (iRate == 0)
	{
		iRate = 20;
	}
	//DownCpu(iFreeDmIndex, iRate);
	Sleep(5000);

	CString strRoleNum = g_IniFile.GetString(L"Set", L"账号角色");//2
	int iRoleNum = 1;
	if (iRoleNum == 0)
	{
		iRoleNum = 1;
	}
	while (true)
	{
		if (m_ScriptSet[iFreeDmIndex].m_GameInfoSt.m_iRoleIndex > iRoleNum)
		{
			DbgOut(L"[sdn] Here break1");
			m_ScriptSet[iFreeDmIndex].m_GameInfoSt.m_iRoleIndex = 1;
			break;
		}
		DbgOut(L"[sdn] 处理当前任务开始xxxxxxxxxxxxxxxxxxxx");
		DealCurTaskProcess(iFreeDmIndex);

		if (ChangeRole(iFreeDmIndex) != 1)
		{
			//开始下一个角色

		}
		Sleep(3000);
	}
	DbgOut(L"[sdn] 设置界面完成");
	//设置界面完成
	m_ScriptSet[iFreeDmIndex].m_GameInfoSt.m_strRunState = L"完成";
	ShowConsoleInfo(iFreeDmIndex, 10);
	//设置配置文件完成
	CString strSet;
	strSet.Format(L"%d", m_ScriptSet[iFreeDmIndex].m_iControlIndex + 1);
	g_AccountFile.SetInt(strSet, L"完成", 1);
	//Account.ini
	/*DbgOut("[sdn] ---------------开始下一个账号----------------");
	::SendMessage((HWND)m_ScriptSet[iFreeDmIndex].m_hControlHandle, WM_USERMONITOR, 2, (LPARAM)iFreeDmIndex);
	for (int N = 0; N<30; N++)
	{
	Sleep(2000);
	}*/

	return 0;
}

void KillProcess(CString sExeName)
{
	HANDLE hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hSnapShot == 0)
		return;
	PROCESSENTRY32 thePE;
	thePE.dwSize = sizeof(PROCESSENTRY32);


	//遍历正在运行的第一个系统进程


	bool Status = Process32First(hSnapShot, &thePE);
	bool bHaveFlag = false;
	DWORD ProcessID = 0;


	while (Status)
	{
		//遍历正在运行的下一个系统进程  
		Status = Process32Next(hSnapShot, &thePE);
		//找到相应的进程 **.exe


		// if(0 == wcscmp(thePE.szExeFile,L""))
		CString sFindName = thePE.szExeFile;
		CString sTemp = sExeName.Mid(0, sFindName.GetLength());


		if (sFindName == sTemp)
		{
			bHaveFlag = true;
			ProcessID = thePE.th32ProcessID;


			////结束指定的进程 ProcessID
			if (!TerminateProcess(OpenProcess(PROCESS_TERMINATE || PROCESS_QUERY_INFORMATION, false, ProcessID), 0))
			{
				//AfxMessageBox(L"无法终止指定的进程！", MB_ICONWARNING || MB_OK);
			}
			else
			{
				//AfxMessageBox(L"进程结束！", MB_ICONWARNING || MB_OK);
				break;
			}
		}
	}
	CloseHandle(hSnapShot);
}
